﻿using System;
using System.Collections.Generic;
using NUnit.Framework;
using Xenta.Enums;
using Xenta.Operations;
using Xenta.Utils;

namespace Xenta.Tests.Operations
{
    [TestFixture]
    public class PublicationTests : OperationTest
    {
        protected override IEnumerable<Type> ModelMappings
        {
            get
            {
                return NHMappings.ALL;
            }
        }

        [TestCase("code")]
        public void Can_CRUD(string code)
        {
            #region Prepare

            var country = Processor.Execute(new CreateCountry
            {
                Code = "xy"
            }).Result;
            var localizationProfile = Processor.Execute(new CreateLocalizationProfile
            {
                Code = "xy",
                LanguageID = Processor.Execute(new CreateLanguage
                {
                    Code = "xy"
                }).Result.EntityID,
                TimeZoneID = Processor.Execute(new CreateTimeZone
                {
                    Code = "xy"
                }).Result.EntityID,
                CurrencyID = Processor.Execute(new CreateCurrency
                {
                    Code = "xy",
                    Rate = 1
                }).Result.EntityID,
                CountryID = country.EntityID,
                RegionID = Processor.Execute(new CreateRegion
                {
                    Code = "xy",
                    CountryID = country.EntityID
                }).Result.EntityID,
                DimensionUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-dim",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Dimension
                }).Result.EntityID,
                WeightUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-weight",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Weight
                }).Result.EntityID,
                InformationUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-info",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Information
                }).Result.EntityID
            }).Result;
            var account = Processor.Execute(new CreateAccount
            {
                Email = "email@example.com",
                Username = "username",
                Password = "password",
                LocalizationProfileID = localizationProfile.EntityID
            }).Result;
            var author = Processor.Execute(new CreatePublicationAuthor
            {
                AccountID = account.EntityID
            }).Result;

            #endregion

            var publication = Processor.Execute(new CreatePublication
            {
                AuthorID = author.EntityID,
                FileGroupID = 0,
                Code = code,
                PublishedOn = DateTime.UtcNow
            }).Result;

            Assert.NotNull(publication);
            publication.Code.AreEqualIgnoringCase(code);
            publication.AreEqual(Processor.Execute(new GetPublication
            {
                EntityID = publication.EntityID
            }).Result);
            publication.AreEqual(Processor.Execute(new GetPublicationByCode
            {
                Code = code
            }).Result);

            code = code.Reverse();
            var upd_publication = Processor.Execute(new UpdatePublication
            {
                EntityID = publication.EntityID,
                FileGroupID = 0,
                AuthorID = author.EntityID,
                Code = code,
                PublishedOn = DateTime.UtcNow,
                Flags = PublicationFlags.Active
            }).Result;

            Assert.NotNull(upd_publication);
            upd_publication.AreEqual(publication);
            upd_publication.Code.AreEqualIgnoringCase(code);
            upd_publication.Flags.AreEqual(PublicationFlags.Active);

            Processor.Execute(new DeletePublication
            {
                EntityID = publication.EntityID
            });
        }

        [Test]
        public void Cannot_Create_If_Code_Empty()
        {
            Assert.Throws<ApplicationException>(() => Processor.Execute(new CreatePublication
            {
                Code = String.Empty
            }));
        }

        [Test]
        public void Can_Manage_TagAndAttribute_Relations()
        {
            #region Prepare

            var country = Processor.Execute(new CreateCountry
            {
                Code = "xy"
            }).Result;
            var localizationProfile = Processor.Execute(new CreateLocalizationProfile
            {
                Code = "xy",
                LanguageID = Processor.Execute(new CreateLanguage
                {
                    Code = "xy"
                }).Result.EntityID,
                TimeZoneID = Processor.Execute(new CreateTimeZone
                {
                    Code = "xy"
                }).Result.EntityID,
                CurrencyID = Processor.Execute(new CreateCurrency
                {
                    Code = "xy",
                    Rate = 1
                }).Result.EntityID,
                CountryID = country.EntityID,
                RegionID = Processor.Execute(new CreateRegion
                {
                    Code = "xy",
                    CountryID = country.EntityID
                }).Result.EntityID,
                DimensionUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-dim",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Dimension
                }).Result.EntityID,
                WeightUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-weight",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Weight
                }).Result.EntityID,
                InformationUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-info",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Information
                }).Result.EntityID
            }).Result;
            var account = Processor.Execute(new CreateAccount
            {
                Email = "email@example.com",
                Username = "username",
                Password = "password",
                LocalizationProfileID = localizationProfile.EntityID
            }).Result;
            var author = Processor.Execute(new CreatePublicationAuthor
            {
                AccountID = account.EntityID
            }).Result;

            #endregion

            var publication = Processor.Execute(new CreatePublication
            {
                Code = "xy",
                AuthorID = author.EntityID,
                FileGroupID = 0,
                PublishedOn = DateTime.UtcNow
            }).Result;

            var tag = Processor.Execute(new CreatePublicationTag()).Result;
            Processor.Execute(new MapPublicationTagToPublication
            {
                ChildID = tag.EntityID,
                EntityID = publication.EntityID
            }).Result.Tags
                .DoesContain(tag);
            Processor.Execute(new UnmapPublicationTagFromPublication
            {
                ChildID = tag.EntityID,
                EntityID = publication.EntityID
            }).Result.Tags
                .DoesNotContain(tag);

            var attr = Processor.Execute(new CreateLookupAttribute
            {
                Flags = LookupAttributeFlags.Publication
            }).Result;
            Processor.Execute(new MapLookupAttributeToPublication
            {
                ChildID = attr.EntityID,
                EntityID = publication.EntityID
            }).Result.Attributes
                .DoesContain(attr);
            Processor.Execute(new UnmapLookupAttributeFromPublication
            {
                ChildID = attr.EntityID,
                EntityID = publication.EntityID
            }).Result.Attributes
                .DoesNotContain(attr);
        }
    }
}
